package com.juc.concurrency.basic.createthread;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * @author huangweiyue
 * @description FutureTask 有返回值的形式，可以拿到任务结果
 * @date Created in 2021-07-28
 */
public class ImplementsCallable {
    public static void main(String[] args) {
        //1.创建FutureTask对象的构造函数为Callable实例
        FutureTask<String> futureTask = new FutureTask<>(new CallTask());
        //2.使用FutureTask作为任务创建一个线程并启动
        new Thread(futureTask).start();
        try {
            //3.get获取返回值
            String result = futureTask.get();
            System.out.println(result);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static class CallTask implements Callable<String> {

        @Override
        public String call() throws Exception {
            return "hello";
        }
    }
}
